//
//  OriginalView.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/13.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

class OriginalView: UIImageView {
    
    var iconView : UIImageView!
    var nameView : UILabel!
    var vipView : UIImageView!
    var timeView : UILabel!
    var sourceView : UILabel!
    var textView : UILabel!
    var photosView : PhotosView!

    var statusFrame : StatusFrame!{
        didSet{
            setUpData()
            setUpFrame()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpAllChindView()
        self.userInteractionEnabled = true
        self.image = UIImage.imageWithStretchableName("timeline_card_top_background")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpAllChindView(){
        //头像
        iconView = UIImageView()
        self.addSubview(iconView)
        //昵称
        nameView = UILabel()
        nameView.font = Coant.NameFont
        self.addSubview(nameView)
        //vip
        vipView = UIImageView()
        self.addSubview(vipView)
        //时间
        timeView = UILabel()
        timeView.font = Coant.TimeFont
        timeView.textColor = UIColor.orangeColor()
        self.addSubview(timeView)
        //来源
        sourceView = UILabel()
        sourceView.font = Coant.SourceFont
        sourceView.textColor = UIColor.lightGrayColor()
        self.addSubview(sourceView)
        //正文
        textView = UILabel()
        textView.font = Coant.TextFont
        textView.numberOfLines = 0
        self.addSubview(textView)
        
        photosView = PhotosView(frame: CGRectMake(0, 0, 0, 0))
        self.addSubview(photosView)
        
    }
    
    func setUpData(){
        let status = self.statusFrame.status
        iconView.kf_setImageWithURL(status.user.profile_image_url, placeholderImage: UIImage(named: "timeline_image_placeholder"))
        
        if status.user.vip {
            nameView.textColor = UIColor.redColor()
        }else{
            nameView.textColor = UIColor.blackColor()
        }
    
        nameView.text = status.user.name
        let imageName = "common_icon_membership_level\(status.user.mbrank)"
        let image = UIImage(named: imageName)
        
        vipView.image = image
        
        // 时间
        timeView.text = status.getcreated_at()
        
        // 来源
        
        sourceView.text = status.source
        
        // 正文
        textView.text = status.text
        
        photosView.pic_urls = status.pic_urls
        
        //print("\(status.pic_urls)\(status.text)")
    }
    
    func setUpFrame(){
        // 头像
        iconView.frame = statusFrame.origninalIconFrame
        
        // 昵称
        nameView.frame = statusFrame.origninalNameFrame
        
        // vip
        if (statusFrame.status.user.vip) { // 是vip
            vipView.hidden = false
            vipView.frame = statusFrame.origninalVipFrame
        }else{
            vipView.hidden = true
        }
        // 时间
        timeView.frame = statusFrame.orignianlTimeFrame
        
        // 来源
        sourceView.frame = statusFrame.orignianlSourceFrame
        
        // 正文
        textView.frame = statusFrame.orignianlTextFrame
        
        photosView.frame = statusFrame.orignianlPhotosFrame
        
    }
    
}
